package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.RecordCourse;
import com.xmy.cultivate.entity.RepairCourse;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 补课表 Mapper 接口
 * </p>
 *
 * @author Coly
 * @since 2023-03-07
 */
@Mapper
public interface RepairCourseMapper extends BaseMapper<RepairCourse> {
    @Select("select * from repair_course where record_course_id = #{id}")
    public abstract RepairCourse select(@Param("id") Long id);

    @Select("select * from repair_course where student_id = #{studentId} and course_type = #{courseType} and year_part = ${yearPart} and `quarter` = ${quarterNum}")
    public abstract List<RepairCourse> selectByStudent(@Param("studentId") Long studentId, @Param("courseType") Integer courseType, @Param("yearPart") Integer yearpart, @Param("quarterNum") Integer quarterNum);

    @Select("select * from record_course where scheduling_detail_id = #{scheduling_detail_id}")
    public abstract List<RecordCourse> selectByscheduling(@Param("scheduling_detail_id") Long schedulingId);

    @Select("delete from repair_course where record_course_id = #{record_course_id}")
    public abstract void deleteByRecordCourseId(@Param("record_course_id") Long recordCoursId);

    /**
     * 按班级调课，查看时间是否宣冲突
     *
     * @param startDateTime
     * @param schoolDate
     * @param gradeId
     * @return
     */
    //@Select("SELECT IFNULL(sum(case WHEN #{startDateTime} BETWEEN start_date_time and end_date_time then 1 ELSE 0 END),0) as count from course_scheduling_detail where school_date = #{schoolDate} and grade_id =#{gradeId} and id<>#{id} and deleted=0")
    @Select("SELECT count(id) as count from course_scheduling_detail where grade_id =#{gradeId} and id<>#{id} and deleted=0 and (start_date_time BETWEEN #{startDateTime} and #{endDateTime} or end_date_time BETWEEN #{startDateTime} and #{endDateTime})")
    public abstract Long getClashCountForGrade(@Param("startDateTime") String startDateTime, @Param("endDateTime") String endDateTime, @Param("schoolDate") String schoolDate, @Param("gradeId") Long gradeId, @Param("id") Long id);

    /**
     * 按学员调课，查看时间是否宣冲突
     *
     * @param startDateTime 时间
     * @param studentId     学生id
     * @return
     */
    @Select("select sum(count) as count from (select count(id) as count from student_grade where deleted = 0 and status = 1 and reading_status=1 and student_id =#{studentId} and grade_id in (select grade_id from course_scheduling_detail where deleted=0 and status<>2 and lesson_type = ${lessonType} and (start_date_time BETWEEN #{startDateTime} and #{endDateTime} or end_date_time BETWEEN #{startDateTime} and #{endDateTime}))\n" +
            "UNION\n" +
            "select count(id) as count from repair_course where student_id = #{studentId} and deleted = 0 and lesson_type = ${lessonType} and (start_date_time BETWEEN #{startDateTime} and #{endDateTime} or end_date_time BETWEEN #{startDateTime} and #{endDateTime})) as t")
    public abstract Long getClashCountForStudent(@Param("startDateTime") String startDateTime, @Param("endDateTime") String endDateTime, @Param("studentId") Long studentId, @Param("lessonType") Integer lessonType);

    @Select("select *,record_course_id as course_id from repair_course ${ew.customSqlSegment}")
    public abstract IPage<RepairCourse> consumeCourseForStudentList(IPage<RecordCourse> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select *,record_course_id as course_id from repair_course ${ew.customSqlSegment}")
    public abstract IPage<RecordCourse> consumeCourseForStudentExcel(IPage<RecordCourse> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select name from staff where id = #{teacher_id}")
    public abstract String getTeacherName(@Param("teacher_id") String teacherId);

    @Select("select name from organization where id = #{school_id}")
    public abstract String getSchoolName(@Param("school_id") String schoolId);

    @Select("SELECT t1.*,t3.`name` AS course_name,t4.`name` AS student_name FROM repair_course AS t1 LEFT JOIN grade AS t2 ON t1.grade_id = t2.id LEFT JOIN course AS t3 ON t2.course_id = t3.id LEFT JOIN student AS t4 ON t1.student_id = t4.id ${ew.customSqlSegment}")
    public abstract IPage<RepairCourse> getTemporaryClass(IPage<RepairCourse> page, @Param(Constants.WRAPPER) Wrapper wrapper);
}
